<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Model\AdminLog;
use App\Model\RefundAddress;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * 系统配置
 */
class SystemController extends Controller
{

    /**
     * 初始化控制器
     */
    public function __construct()
    {

    }

    /**
     * 退款地址列表
     */
    public function refund_address_list(Request $request)
    {
        // 获取参数
        $params = $request->all();
        // 设置对象
        $model = (new RefundAddress());
        // id
        if (isset($params['address_id']) && $params['address_id']) {
            $model = $model->where('id', $params['address_id']);
        }
        // 收货人
        if (isset($params['contacts']) && $params['contacts']) {
            $model = $model->where('contacts', 'like', '%' . $params['contacts'] . '%');
        }
        // 手机号
        if (isset($params['mobile']) && $params['mobile']) {
            $model = $model->where('mobile', 'like', '%' . $params['mobile'] . '%');
        }
        // 创建时间
        if (!empty($params['start_createtime']) && !empty($params['end_createtime'])) {
            $model = $model->whereBetween('createtime', [$params['start_createtime'], $params['end_createtime']]);
        }
        // 修改时间
        if (!empty($params['start_updatetime']) && !empty($params['end_updatetime'])) {
            $model = $model->whereBetween('updatetime', [$params['start_updatetime'], $params['end_updatetime']]);
        }
        // 获取条数
        $pageSize = isset($params['pageSize']) && $params['pageSize'] ? $params['pageSize'] : 10;
        $total = $model->count();
        $list = $model->orderBy('id', 'desc')
            ->take($pageSize)
            ->get(['id as address_id', 'contacts', 'mobile', 'province', 'city', 'area', 'address',
                'createtime', 'updatetime'])
            ->toArray();
        $this->success(['total_count' => $total, 'total_page' => ceil($total / $pageSize), 'list' => $list]);
    }

    /**
     * 添加退款地址
     */
    public function add_refund_address(Request $request)
    {
        // 获取参数
        $only = ['contacts', 'mobile', 'province', 'city', 'area', 'address'];
        $params = $request->only($only);
        if ($params) {
            // 规则验证
            $validate = (new \App\Validate\Admin\RefundAddress());
            if (!$validate->check($params, [], [], 'add_refund_address')) {
                $this->error($validate->getError());
            }
            DB::beginTransaction();
            try {
                $model = (new RefundAddress());
                foreach ($params as $key => $val) {
                    if (isset($val)) {
                        $model->$key = $val;
                    }
                }
                $result = $model->save();
                if ($result !== false) {
                    DB::commit();
                } else {
                    throw new \Exception('操作失败');
                }
            } catch (\Exception $exception) {
                DB::rollBack();
                $this->error($exception->getMessage());
            }
            if ($result !== false) {
                $this->success();
            } else {
                $this->error('未插入任何行');
            }
        }
        $this->error('参数不能为空');
    }

    /**
     * 退款地址详情
     */
    public function refund_address_details(Request $request)
    {
        // 获取参数
        $ids = $request->input('address_id');
        $row = (new RefundAddress())->where('id', $ids)->first();
        if (!$row) {
            $this->error('记录未找到');
        }
        $this->success($row);
    }

    /**
     * 编辑退款地址
     */
    public function edit_refund_address(Request $request)
    {
        // 获取参数
        $only = ['address_id', 'contacts', 'mobile', 'province', 'city', 'area', 'address'];
        $params = $request->only($only);
        if ($params) {
            // 规则验证
            $validate = (new \App\Validate\Admin\RefundAddress());
            if (!$validate->check($params, [], [], 'edit_refund_address')) {
                $this->error($validate->getError());
            }
            DB::beginTransaction();
            try {
                $model = (new RefundAddress());
                $refundAddress = $model::find($params['address_id']);
                unset($params['address_id']);
                foreach ($params as $key => $val) {
                    if (isset($val)) {
                        $refundAddress->$key = $val;
                    }
                }
                $result = $refundAddress->save();
                if ($result !== false) {
                    Db::commit();
                } else {
                    throw new \Exception('操作失败');
                }
            } catch (\Exception $exception) {
                DB::rollBack();
                $this->error($exception->getMessage());
            }
            if ($result !== false) {
                $this->success();
            } else {
                $this->error('未更新任何行');
            }
        }
        $this->error('参数不能为空');
    }

    /**
     * 删除退款地址
     */
    public function del_refund_address(Request $request)
    {
        // 获取参数
        $ids = $request->input('address_id');
        if ($ids) {
            $count = 0;
            DB::beginTransaction();
            try {
                $model = (new RefundAddress());
                $list = $model::find(explode(',', $ids));
                foreach ($list as $k => $v) {
                    $count += $v->delete();
                }
                Db::commit();
            } catch (\Exception $exception) {
                DB::rollBack();
                $this->error($exception->getMessage());
            }
            if ($count) {
                $this->success($count);
            } else {
                $this->error(__('未删除任何行'));
            }
        }
        $this->error('参数不能为空');
    }

    /**
     * 管理员日志列表
     */
    public function admin_log_list(Request $request)
    {
        // 获取参数
        $params = $request->all();
        // 设置对象
        $model = (new AdminLog());
        // id
        if (isset($params['log_id']) && $params['log_id']) {
            $model = $model->where('id', $params['log_id']);
        }
        // 管理员名字
        if (isset($params['username']) && $params['username']) {
            $model = $model->where('username', 'like', '%' . $params['username'] . '%');
        }
        // 日志标题
        if (isset($params['title']) && $params['title']) {
            $model = $model->where('title', 'like', '%' . $params['title'] . '%');
        }
        // 操作页面
        if (isset($params['url']) && $params['url']) {
            $model = $model->where('url', 'like', '%' . $params['url'] . '%');
        }
        // ip
        if (isset($params['ip']) && $params['ip']) {
            $model = $model->where('ip', 'like', '%' . $params['ip'] . '%');
        }
        // 创建时间
        if (!empty($params['start_createtime']) && !empty($params['end_createtime'])) {
            $model = $model->whereBetween('createtime', [$params['start_createtime'], $params['end_createtime']]);
        }
        // 获取条数
        $pageSize = isset($params['pageSize']) && $params['pageSize'] ? $params['pageSize'] : 10;
        $total = $model->count();
        $list = $model->orderBy('id', 'desc')
            ->take($pageSize)
            ->get(['id as log_id', 'username', 'title', 'url', 'content', 'ip', 'useragent', 'createtime'])
            ->toArray();
        $this->success(['total_count' => $total, 'total_page' => ceil($total / $pageSize), 'list' => $list]);
    }
}
